#include <cstdlib>
#include <cstring>
#include "mystr.h"

mystr::mystr(size_t increment)
{
    this->increment = increment;
    capacity = increment;
    position = 0;
    buf = (char *)malloc(capacity * sizeof(char));
}

void mystr::append(char c)
{
    if (position == capacity)
    {
        size_t new_capacity = capacity + increment;
        char *temp = (char *)malloc(new_capacity * sizeof(char));
        memcpy(temp, buf, capacity);
        free(buf);
        buf = temp;
        capacity = new_capacity;
    }
    *(buf + position) = c;
    ++position;
}

void mystr::append(const char *str)
{
    for (const char *i = str; *i != 0; i++)
    {
        append(*i);
    }
}

char *mystr::str()
{
    for (size_t i = position; i < capacity; i++)
    {
        *(buf + i) = 0;
    }
    return buf;
}